Repository = function () {
    this.count = function (target, completeFn) {
        $.ajax({
            url: '../Query/Count?target=' + target,
            type: 'Post',
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                if (data.Success) completeFn(data.Result);
                else apprise(data.Error);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                apprise(errorThrown);
            }
        });
    };

    this.get = function (object, completeFn) {
        $.ajax({
            url: '../Query/Query',
            type: 'Post',
            dataType: 'json',
            data: JSON.stringify(object),
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                if (data.Success) completeFn(data.Result);
                else apprise(data.Error);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                apprise(errorThrown);
            }
        });
};
};

function getMembers(obj) {
	var members = new Array();
	var i = 0;

	for (var member in obj) members[i++] = member;
	return members.sort();
}

function getMemberString(obj) {
    var members = getMembers(obj);
    var result = '';

	for (var i = 0; i < members.length; i++) result += members[i] + ';';
    return result;
}

jQuery.fn.modeButton = function () {
    var dropdown = $(this);
    dropdown.dropdown();

    $('li ul li a', this).each(function (index, element) {
        $(element).click(function () {
            $('li', dropdown).contents().first().get(0).textContent = $(element).text();
        //alert($('li ul:first', this).html());
            $('li ul:first', dropdown).css('visibility', 'hidden');
        });
    });
};

jQuery.fn.dropdown = function () {
    var dropdown = $(this);

    $(this).addClass('dropdown');
    $('li ul', this).addClass('sub_menu');
    $('li', this).addClass('ui-widget-header');
    $('li', this).addClass('ui-state-default');
    $(this).children('li').addClass('ui-corner-all');

    $("ul.dropdown li").hover(function () {
        $(this).addClass("ui-state-hover");
        $('ul:first', this).css('visibility', 'visible');
    }, function () {
        $(this).removeClass("ui-state-hover");
        $('ul:first', this).css('visibility', 'hidden');
    });
    $("ul.dropdown li ul li:has(ul)").find("a:first").append(" &raquo; ");
};

jQuery.jsonQuery = function (object, completeFn) {
    $.ajax({
        url: '../Query/Query',
        type: 'Post',
        dataType: 'json',
        data: JSON.stringify(object),
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            if (data.Success) completeFn(data.Result);
            else apprise(data.Error);
        },
        error: function (jqXHR, textStatus, errorThrown) {
            apprise(errorThrown);
        }
    });
};

jQuery.jsonSave = function (url, object, completeFn) {
    $.ajax({
        url: url,
        type: 'Post',
        dataType: 'json',
        data: JSON.stringify(object),
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            if (data.Success) {
                var newObject;
                eval('newObject = ' + data.Model);

                apprise('Saved successfully');
                if (completeFn)
                    completeFn(newObject);
            }
            else apprise(data.Error);
        },
        error: function (jqXHR, textStatus, errorThrown) {
            apprise(errorThrown);
        }
    });
};